package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cecsmsserve.entity.ServiceOrder;
import com.cecsmsserve.mapper.ServiceOrderMapper;
import com.cecsmsserve.service.IServiceOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.util.ValidateUtil;
import com.cecsmsserve.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王郝浠
 * @since 2024-04-17
 */
@Service
public class ServiceOrderServiceImpl extends ServiceImpl<ServiceOrderMapper, ServiceOrder> implements IServiceOrderService {

    @Autowired
    ServiceOrderMapper mapper;

    @Override
    public CommonResult selectAll() {
        List<ServiceOrder> list=mapper.selectList(null);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectAllByPage(int current, int size) {
//        //创建IPage对象，设置参数：第几页、每页显示最大行数
//        IPage<ServiceOrder> page=new Page<>(current,size);
//        //调用selectPage方法进行分页查询，返回值为IPage对象
//        IPage<ServiceOrder> serviceOrderIPage=baseMapper.selectPage(page,null);
//        CommonResult result=new CommonResult(serviceOrderIPage);
//        System.out.println(result);
//        if (serviceOrderIPage==null||serviceOrderIPage.getSize()==0){
//            result.setNotFound();
//        }
        int start=(current-1)*size;
        List<ServiceOrder> list=mapper.selectAllByPage(start,size);
        List<ServiceOrder> getTotal=mapper.selectList(null);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectServiceByPage(int current, int size) {
        int start=(current-1)*size;
        List<ServiceOrder> list=mapper.selectServiceByPage(start,size);
        QueryWrapper<ServiceOrder> wrapper=new QueryWrapper<>();
        wrapper.ne("typeBId",4);
        List<ServiceOrder> getTotal=mapper.selectList(wrapper);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectHealthByPage(int current, int size) {
        int start=(current-1)*size;
        List<ServiceOrder> list=mapper.selectHealthByPage(start,size);
        QueryWrapper<ServiceOrder> wrapper=new QueryWrapper<>();
        wrapper.eq("typeBId",4);
        List<ServiceOrder> getTotal=mapper.selectList(wrapper);
        int total=getTotal.size();
        CommonResult result=new CommonResult(String.valueOf(total),list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByUId(int uId) {
        List<ServiceOrder> list=mapper.selectByUId(uId);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByuIdByState(int uId, int state) {
        List<ServiceOrder> list=mapper.selectByuIdByState(uId,state);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult selectByState(int orderState) {
        List<ServiceOrder> list=mapper.selectByState(orderState);
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }

    @Override
    public CommonResult insert(ServiceOrder serviceOrder) {
        try {
            serviceOrder.setId(null);
            serviceOrder.setOrderState(0);
            serviceOrder.setOrderDate(java.time.LocalDate.now());
            serviceOrder.setOrderTime(java.time.LocalTime.now());
            int i = mapper.insert(serviceOrder);
            CommonResult result = new CommonResult(serviceOrder);
            if (i <= 0) {
                result.setNotInserted();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(0, "订单创建失败：" + e.getMessage(), null);
        }
    }

    @Override
    public CommonResult update(ServiceOrder serviceOrder) {
        // 验证电话号码格式
        if (serviceOrder.getTelephone() != null && !ValidateUtil.isValidPhone(serviceOrder.getTelephone())) {
            return new CommonResult(0, "电话号码格式不正确", null);
        }
        
        int i = mapper.updateById(serviceOrder);
        CommonResult result = new CommonResult(i);
        if (i <= 0) {
            result.setNotUpdate();
        }
        return result;
    }

    @Override
    public CommonResult countRate() {
        List<HashMap<String,Object>> list=mapper.countRate();
        CommonResult result=new CommonResult(list);
        if(list==null||list.size()==0){
            result.setNotFound();
        }
        return result;
    }
}
